'use strict';

const Service = require('egg').Service;
const { Op } = require('sequelize');
const moment = require("moment");

class InviteFriendService extends Service {

    async getUnlockFriend(user_id) {
        return await this.ctx.model.InviteFriend.findAll({
            include: [
                {
                    model: this.ctx.model.User,
                    as: 'friend',
                    attributes: ['id', 'nickname', 'avatar']
                }
            ],
            where: {
                user_id,
                unlock: false
            }, 
            order: [
                ['create_time', 'desc']
            ],
        })
    }

    async getFriends(user_id, page = 0, size = 15) {
        const {rows, count} = await this.ctx.model.InviteFriend.findAndCountAll({
            include: [
                {
                    model: this.ctx.model.User,
                    as: 'friend',
                    attributes: ['id', 'nickname', 'avatar']
                }
            ],
            where: {
                user_id
            }, 
            order: [
                ['create_time', 'desc']
            ],
            offset: page * size,
            limit: size
        });
        return {rows, count};
    }

    async add(user_id, friend_id) {
        return await this.ctx.model.InviteFriend.create({
            user_id, friend_id
        })
    }

    async updateUnlock(user_id) {
        return await this.ctx.model.InviteFriend.update({
                unlock: true
            }, {
                where: {
                    user_id
                }
            })
    }
}

module.exports = InviteFriendService;